package com.franklin.ideaplugin.easytesting.ui.execute;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.franklin.ideaplugin.api.beans.DateClass;
import com.franklin.ideaplugin.api.utils.SwingUtils;
import com.franklin.ideaplugin.easytesting.ui.Sizes;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.psi.PsiParameter;
import com.intellij.ui.components.JBTextField;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author Ye Junhui
 * @since 2023/7/2 16:51
 */
public class DateClassParameterFormProvider implements IParameterFormProvider {

    private final static String[] DATE_PATTERNS;
    private final static List<String> DATE_PATTERN_LIST;

    static {
        Field[] declaredFields = DatePattern.class.getDeclaredFields();
        DATE_PATTERN_LIST = Arrays.stream(declaredFields)
                .filter(field -> field.getType().equals(String.class))
                .peek(field -> field.setAccessible(true))
                .map(field -> {
                    try {
                        return field.get(DatePattern.class).toString();
                    } catch (IllegalAccessException e) {
                        return "";
                    }
                })
                .filter(StrUtil::isNotBlank)
                .distinct()
                .collect(Collectors.toList());
        DATE_PATTERNS = DATE_PATTERN_LIST.toArray(new String[]{});
    }

    @Override
    public boolean isTarget(@NotNull Object defaultValue) {
        return defaultValue instanceof DateClass;
    }

    @Override
    public ParameterTextComponent getForm(PsiParameter psiParameter, Object defaultValueOfPsiType, Object defaultValue) {
        DateClass dateClass = (DateClass) defaultValueOfPsiType;
        if (Objects.nonNull(defaultValue) && defaultValue instanceof String){
            dateClass = JSON.parseObject(defaultValue.toString(),DateClass.class);
        }
        if (Objects.isNull(dateClass)){
            dateClass = (DateClass) defaultValueOfPsiType;
        }

        //格式选择器
        ComboBox<String> comboBox = new ComboBox<>(DATE_PATTERNS);
        //最终日期
        JBTextField textField = new JBTextField(dateClass.getDateValue());

        comboBox.setToolTipText("Select a date format to reformat date");
        comboBox.addActionListener(e -> {
            Object selectedItem = comboBox.getSelectedItem();
            if (Objects.nonNull(selectedItem)) {
                String pattern = selectedItem.toString();
                textField.setText(DateUtil.format(new Date(),pattern));
            }
        });
        comboBox.setSelectedIndex(DATE_PATTERN_LIST.indexOf(dateClass.getDatePattern()));



        JPanel jPanel = new JPanel();
        jPanel.setMaximumSize(Sizes.ExecuteMethodFormSize.CHILD_SIZE);
        jPanel.setPreferredSize(Sizes.ExecuteMethodFormSize.CHILD_SIZE);
        jPanel.setLayout(SwingUtils.createLayoutManager(2,1));
        SwingUtils.addCell(jPanel,comboBox,0,0,new Dimension(-1,-1));
        SwingUtils.addCell(jPanel,textField,1,0,new Dimension(-1,-1));

        DateClass finalDateClass = dateClass;
        return new ParameterTextComponent(jPanel) {
            @Override
            public String getText() {
                DateClass newDate = new DateClass();
                newDate.setDatePattern(comboBox.getSelectedItem().toString());
                newDate.setDateValue(textField.getText());
                newDate.setQualifiedName(finalDateClass.getQualifiedName());
                return newDate.toJsonString();
            }

            @Override
            public String getTypeStr(PsiParameter psiParameter) {
                return com.franklin.ideaplugin.easytesting.common.entity.DateClass.class.getCanonicalName();
            }
        };
    }

}
